﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using MossmanMill.DataModel;
using MossmanMill.Properties;
using MossmanMill.View;

namespace MossmanMill
{
    partial class MainForm
    {

        private Slottable selectedSlottable;
        private Card selectedCard;

        private int freeChannels;
        private Rectangle cardDrawRect, sensorDrawRect;

        public void initCard(Card card)
        {
            level = Level.CARD;
            sensors = new Sensor[card.GetChannels()];
            foreach (var s in db.GetSensors(card))
            {
                sensors[s.Channel] = s;
            }
            freeChannels = 0;
            foreach (var s in sensors) if (s == null) freeChannels++;
            cardDrawRect = new Rectangle(new Point(40, 40), new Size(80, card.GetChannels() / 2 * 20 + 50)); 
            sensorDrawRect = new Rectangle(new Point(cardDrawRect.Right + 40, 120),new Size(viewPanel.Width - (cardDrawRect.Right + 80), 80));

            int numCh = selectedCard.GetChannels();
            menuEditDataInput.Enabled = true;
            menuEditDragDrop.Enabled = true;
        }

        private void DrawCard(Graphics g)
        {
            selectedCard.draw(g, 40, 40, 1.0f, false);
            var sensor = selectedCard.SelectedSensor;
            if (sensor != null) sensor.draw(g, 130, 50);
            DrawBackButton(g);
        }

        private void ClickCard(Point c, int type)
        {
            bool invalidate = false;
            if (selectedCard.interact(c, type, 40, 40, 1.0f, false)) invalidate = true;
            var sensor = selectedCard.SelectedSensor;
            if (sensor != null) if (sensor.interact(c, type, 130, 50)) invalidate = true;
            if (invalidate) viewPanel.Invalidate();
            if (backRect.Contains(c) && type == 2)
            {
                initRack(selectedRack);
                viewPanel.Invalidate();
                return;
            }
        }

    }
}
